package cn.yunhe.service.impl;

import cn.yunhe.mapper.UserInfoPMapper;
import cn.yunhe.mapper.UserpMapper;
import cn.yunhe.pojo.*;
import cn.yunhe.service.UsersService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UsersServiceImpl implements UsersService {
    @Autowired
    private UserpMapper userpMapper;

    @Autowired
    private UserInfoPMapper userInfoPMapper;


    @Override

    public PageBean listDeptOfPage(PageBean pageBean) {


        int start = (pageBean.getCurPage() - 1) * pageBean.getPageSize();
        // 设置分页sql 条件
//        PageHelper.offsetPage(start, pageBean.getPageSize());
        PageHelper.startPage(pageBean.getCurPage(), pageBean.getPageSize());


        List<Userp> userps = userpMapper.listDeptAndParent();

        // 分页插件 类 PageInfo
        PageInfo<Userp> pageInfo = new PageInfo<>(userps);

        pageBean.setDatas(pageInfo.getList());  // 分装分页数据
        pageBean.setTotalPages(pageInfo.getPages());  // 共多少页

        pageBean.setTotalRows(pageInfo.getTotal());


        return pageBean;
    }

    @Override
    public List<DeptP> listDept() {

        List<Userp> userps = userpMapper.listDeptAndParent();
        return null;
    }

    @Override
    public Userp findUserById(String id) {

        return userpMapper.findUserById(id);
    }

    @Override
    public int insertUserAndUserInf(Userp userp, UserInfoP userInfoP) {

        Subject subject = SecurityUtils.getSubject();//获取当前登录者的信息

        CurrentUser currentUser = (CurrentUser) subject.getPrincipal();

        String userid = UUID.randomUUID().toString();
        userp.setUserId(userid);
        String salt = userp.getUserName() + userp.getUserId();
        String password = "111";
        userp.setPassword(new Md5Hash(password, salt, 3).toString());

        Userp loginUserP = currentUser.getUserP();//获取到登录人的信息

        DeptP logindeptP = currentUser.getDeptP();//获取到登录人所在部门信息

        userp.setCreateBy(loginUserP.getUserId());//创建人
        userp.setCreateDept(logindeptP.getDeptId());//创建部门
        userp.setUpdateBy(loginUserP.getUserId());//更新人
        userp.setCreateTime(new Date());//创建时间
        userp.setUpdateTime(new Date());//更


        int re = userpMapper.insertSelective(userp);

        userInfoP.setUserInfoId(userid);
        userInfoP.setCreateBy(loginUserP.getUserId());//创建人
        userInfoP.setCreateDept(logindeptP.getDeptId());//创建部门
        userInfoP.setUpdateBy(loginUserP.getUserId());//更新人
        userInfoP.setCreateTime(new Date());//创建时间
        userInfoP.setUpdateTime(new Date());//更新时间


        int re2 = userInfoPMapper.insertSelective(userInfoP);

        return re;
    }

    @Override
    public int updateUserAndUserIp(Userp userp, UserInfoP userInfoP) {
        int i = userInfoPMapper.updateByPrimaryKeySelective(userInfoP);
        int i1 = userpMapper.updateByPrimaryKeySelective(userp);
        return i;
    }

    @Override
    public GllsResult deleteUserByUsertId(String[] userIds) {

        for (int i = 0; i < userIds.length; i++) {
            String userId = userIds[i];
            userpMapper.deleteByPrimaryKey(userId);
        }

        return new GllsResult(200,null,"删除成功");
    }


}
